#include "list.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
LTNode* ListInit() {
	LTNode* head = (LTNode*)malloc(sizeof(LTNode));
	assert(head);
	head->prev = head;
	head->next = head;
	return head;
}
LTNode* BuyListNode(ListDataType x) {
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	assert(newnode);
	newnode->prev = NULL;
	newnode->next = NULL;
	newnode->data = x;
	return newnode;
}
void ListPushBack(LTNode* plist, ListDataType x) {
	assert(plist);
	LTNode* newnode = BuyListNode(x);
	LTNode* tail = plist->prev;
	tail->next = newnode;
	newnode->prev = tail;
	newnode->next = plist;
	plist->prev = newnode;
}
void ListPrint(LTNode* plist){
	assert(plist);
	if (plist->next == plist) return;
	LTNode* cur = plist->next;
	while (cur != plist) {
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
LTNode* ListFind(LTNode* plist, ListDataType x) {
	assert(plist);
	LTNode* cur = plist->next;
	while (cur != plist) {
		if (cur->data == x)return cur;
		cur = cur->next;
	}
	return NULL;
}
void ListInsert(LTNode* pos, ListDataType x) {
	assert(pos);
	LTNode* next = pos->next;
	LTNode*newnode=BuyListNode(x);
	pos->next = newnode;
	newnode->prev = pos;
	newnode->next = next;
	next->prev = newnode;
}
void ListErase(LTNode* pos) {
	assert(pos);
	LTNode* prev = pos->prev;
	LTNode* next = pos->next;
	free(pos);
	pos = NULL;
	prev->next = next;
	next->prev = prev;
}
void ListDestroy(LTNode* plist) {
	LTNode* cur = plist->next;
	while (cur != plist) {
		LTNode* next = cur->next;
		free(cur);
		cur = next;
	}
}
